import { API_LIST } from '../../../Api/Api'
import { commonTableList } from '../Common/common'
import { AddDataButton, DelDataButton, AnnexButton, SaveFilesUrl } from '../Common/options'

export const AttachmentDetails = async ({ OptionsEvent, oneData, Permission, actionRef, tableName, tableNameList }) => {
    let dataList = {
        pageSize: 10,
        hideInSearch: true,
        params: { annexUuid: oneData.uuid, OrderField: 'tableName@asc' },
        initialValues: { uuid: oneData.uuid, annexUuid: oneData.uuid },
        get: API_LIST()['getAttachmentDetails'],
        added: 'posAttachmentDetails',
        options: [<AddDataButton OptionsEvent={OptionsEvent} type="primary" icon="add" title="新增附件" />],
        columns: [
            {
                title: 'uuid',
                dataIndex: 'uuid',
                valueType: 'hidden',
                hideInTable: true,
            },
            {
                title: 'annexUuid',
                dataIndex: 'annexUuid',
                valueType: 'hidden',
                hideInTable: true,
            },
            {
                title: '文件名称',
                dataIndex: 'originalName',
                valueType: 'input',
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
            },
            {
                title: '附件类型',
                dataIndex: 'tableName',
                valueType: 'select',
                valueEnum: tableNameList,
                hideInSearch: !!!tableNameList,
                hideInTable: !!!tableNameList,
                hideInAdded: !!!tableNameList,
                hideInModify: !!!tableNameList,
            },
            {
                dataIndex: 'attachmentUuid',
                title: '文件附件',
                valueType: 'upload',
                valueFile: [1, 2, 3, 4],
                fileUrl: 'fileUrl',
                rules: true,
                hideInTable: true,
            },
            {
                dataIndex: 'fileUrl',
                title: '文件附件',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
                hideInAdded: true,
                hideInModify: true,
                width: 400,
            },
            commonTableList.createUserName,
            commonTableList.createTime,
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 300,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { fileUrl, originalName } = record
                    return [
                        fileUrl && <AnnexButton OptionsEvent={OptionsEvent} key="see" url={fileUrl} title="查看附件" />,
                        fileUrl && (
                            <SaveFilesUrl
                                OptionsEvent={OptionsEvent}
                                key="down"
                                url={fileUrl}
                                originalName={originalName}
                                title="下载附件"
                            />
                        ),
                        <DelDataButton
                            OptionsEvent={OptionsEvent}
                            key="del"
                            title="删除"
                            url="AttachmentDetails"
                            uuid={record.uuid}
                        />,
                    ]
                },
            },
        ],
    }

    return dataList
}
